package org.example.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.dao.dao.user.UTrolleyDao;
import org.example.dao.entity.user.UTrolley;
import org.example.service.user.TrolleyService;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 22449
 */
@Service("trolleyService")
@Slf4j
public class TrolleyServiceImpl implements TrolleyService {


    @Resource
    private IdWorker idWorker;

    @Resource
    private ContextUtil contextUtil;

    @Resource
    private UTrolleyDao uTrolleyDao;



    @Override
    public String deleteTrolley(Long id) {
        Long trolleyId = id;
        UTrolley uTrolley = uTrolleyDao.selectById(trolleyId);
        if(uTrolley==null){
            return "删除失败";
        }else{
            uTrolley.setUpdateTime(new Date());
            uTrolley.setIsValid(0);
            uTrolleyDao.updateById(uTrolley);
            return "删除成功";
        }
    }

    @Override
    public List<UTrolley> getTtrolley() {
        Long id = contextUtil.getId();
        LambdaQueryWrapper<UTrolley> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UTrolley::getUserId, id);
        return uTrolleyDao.selectList(lambdaQueryWrapper);
    }

    @Override
    public void updateTrolleyNum(Long id, Integer num) {
        Long trolleyId = Long.valueOf(id);
        UTrolley uTrolley = uTrolleyDao.selectById(trolleyId);
        uTrolley.setNum(num);
        uTrolley.setUpdateTime(new Date());
        uTrolleyDao.updateById(uTrolley);
    }

    @Override
    public UTrolley addTrolley(Long userId, Long storeId, int num) {
        Long id = idWorker.nextId();
        UTrolley uTrolley = new UTrolley(id, (userId), (storeId), "购物车中", num);
        uTrolleyDao.insert(uTrolley);
        return uTrolley;
    }


}
